// shopPages/clothYe/cloth.js
var sliderWidth = 40;
var app = getApp()
var reclist=[]
var seasonlist=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    imgUrls: ['https://cdnimg.51jlkj.com/mingpian/20190417/08da37d8bcea660b201d6418b74edecaf715a187.jpg',
      'https://cdnimg.51jlkj.com/mingpian/20190416/bc86da92a4e5913937e55f233f2d5853fc1c122b.png'],
    tabs: ["为我推荐", "当季热销"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    rec_more_status: false,
    rec_page: 1,
    season_more_status: false,
    season_page: 1,
    dots_f:false,
    currentSwiper: 0,
    currentSwiper1: 0,
  },
  // 轮播指示点
  swiperChange: function (e) {
    this.setData({
      currentSwiper1: e.detail.current,
    })
  },
  swiperchange: function (e) {
    this.setData({
      currentSwiper: e.detail.current,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    reclist = [];
    seasonlist = [];
    var that = this;
    var rec_more_status = that.data.rec_more_status;
    var season_more_status = that.data.season_more_status;
    var rec_page = that.data.rec_page;
    var season_page = that.data.season_page;
    console.log(options.title);
    if (options.title != undefined){
      wx.setNavigationBarTitle({
        title: options.title
      })
    }
    that.setData({
      p_cat_id: options.cat_id
    });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    var url2 = "/mall/mallIndexSecond"
    var params2 = {
      pid: options.cat_id,
      city_code: app.globalData.currentCode,
      rec_page: rec_page,
      season_page: season_page
    };
    console.log(params2);
    wx.showLoading({
      title: '玩命加载中',
    })

    app.HttpPostSend(url2, params2, function (res) {
      wx.hideLoading();
      if (res.msg !== "success") {
        app.showTipMsg(res.msg);
      } else {
        var rec_list = res.recommend_list.lists;
        if (rec_list.length < 10) {
          rec_more_status = false;
        } else {
          rec_more_status = true;


          rec_page += 1;
          that.setData({
            rec_page: rec_page
          });
        }

        for (let i = 0; i < rec_list.length; i++) {
          reclist.push(rec_list[i]);
        }

        var season_list = res.season_list.lists;
        if (season_list.length < 10) {
          season_more_status = false;
        } else {
          season_more_status = true;


          season_page += 1;
          that.setData({
            season_page: season_page
          });
        }

        for (let i = 0; i < season_list.length; i++) {
          seasonlist.push(season_list[i]);
        }

        that.setData({
          season_more_status: season_more_status,
          rec_more_status: rec_more_status,
          imgUrls: res.banner_list,
          grids: res.cat_list,
          home:res.new_list.lists.data,
          zhinen:res.hot_list.lists.data,
          s_list: reclist,
          s_list1: seasonlist,
          act_img: res.act_img.img
        })
      }
    });
  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  getMore:function(state){
    var that = this;
    var rec_more_status = that.data.rec_more_status;
    var season_more_status = that.data.season_more_status;
    var rec_page = that.data.rec_page;
    var season_page = that.data.season_page;
    var params2 = {
      pid: that.data.p_cat_id,
      city_code: app.globalData.currentCode,
      state: state
    };
    if (state>0){
      var page = that.data.season_page;
      params2.is_season=1;
      params2.pageno = season_page;
    }else{
      var page = that.data.rec_page;
      params2.is_recommend = 1;
      params2.pageno = rec_page;
    }
    var url2 = "/mall/mallIndexSecondMore"

    wx.showLoading({
      title: '玩命加载中',
    })
    app.HttpPostSend(url2, params2, function (res) {
      wx.hideLoading();
      if (res.msg !== "success") {
        app.showTipMsg(res.msg);
      } else {
        if (state > 0) {
          var season_list = res.list.lists;
          if (season_list.length < 4) {
            season_more_status = false;
          } else {
            season_more_status = true;


            season_page += 1;
            that.setData({
              season_page: season_page
            });
          }

          for (let i = 0; i < season_list.length; i++) {
            seasonlist.push(season_list[i]);
          }

          that.setData({
            s_list1: seasonlist,
            season_more_status: season_more_status,
          })
        }else{
          var rec_list = res.list.lists;
          if (rec_list.length < 4) {
            rec_more_status = false;
          } else {
            rec_more_status = true;


            rec_page += 1;
            that.setData({
              rec_page: rec_page
            });
          }

          for (let i = 0; i < rec_list.length; i++) {
            reclist.push(rec_list[i]);
          }


          that.setData({
            s_list: reclist,
            rec_more_status: rec_more_status,
          })
        }

      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.activeIndex==0){
      if (that.data.rec_more_status == true) {
        // 显示加载图标
        wx.showLoading({
          title: '玩命加载中',
        })
        // 页数+1
        that.getMore(0);

      } else {
        wx.hideLoading();
      }
    }else{
      if (that.data.season_more_status == true) {
        // 显示加载图标
        wx.showLoading({
          title: '玩命加载中',
        })
        // 页数+1
        that.getMore(1);

      } else {
        wx.hideLoading();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  toGoodsList: function (e) {
    var cat_id = e.currentTarget.dataset.cat_id;
    wx.navigateTo({
      url: '../../shopPages/storeList/storeList?cat_id=' + cat_id,
    })
  },
  toMore: function (e) {
    var param = e.currentTarget.dataset.param;
    wx.navigateTo({
      url: '../../shopPages/shoplist/shoplist' + param,
    })
  },
  toGoodsDetail: function (e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  },
  menuAction:function(e){

  },
  toGoodsDetail_:function(e){
    wx.navigateTo({
      url: "/pages/goodsDetail/goodsDetail?id="+e.currentTarget.dataset.id
    })
  }
})